<?php

namespace App\Models;

use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Auth;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements JWTSubject
{
    use HasRoles;
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'password', 'nickname', 'realname', 'phone', 'avatar', 'introduction', 'address', 'remark',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    // Rest omitted for brevity

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }

    public function companies()
    {
        return $this->belongsToMany(Company::class, 'company_users', 'user_id', 'company_id');
    }

    public function prizeRecords()
    {
        return $this->hasMany(PrizeRecord::class, 'user_id');
    }

    public function managerRedeemLogs()
    {
        return $this->hasMany(RedeemLog::class, 'company_userid');
    }

    public function redeemLogs()
    {
        return $this->hasMany(RedeemLog::class, 'user_id');
    }

    public function isStaffOf(Company $compony)
    {
        return $compony->users->contains($this);
    }

    public function isOwnerOf($model)
    {
        return $this->id == $model->user_id;
    }
}
